#pragma once

#include <iostream>
#include <sys/epoll.h>
#include "Log.hpp"
#include "Common.hpp"

using namespace LogModule;
namespace epollModule
{
    class epoller
    {
    public:
        epoller():_epfd(-1)
        { }
        ~epoller()
        { }
        void init()
        {
            _epfd = ::epoll_create(256);
            if (_epfd < 0)
            {
                LOG(LogLevel::ERROR) << "epoller create error";
                exit(EPOLL_CREATE_ERR);
            }
                LOG(LogLevel::INFO) << "epoller create success";
        }

        int  wait(struct epoll_event revs[], int num, int timeout) // 输出就绪的fd, 的events
        {
            int n = ::epoll_wait(_epfd, revs, num, timeout);
            if (n < 0)
            {
                LOG(LogLevel::WARNING) << "epoll_wait error";
            }
            return n;
        }
        void ctl(int sockfd, uint32_t events)
        {
            epoll_event ev;
            ev.data.fd = sockfd;
            ev.events =  events;
            int n = ::epoll_ctl(_epfd, EPOLL_CTL_ADD, sockfd, &ev);
            if (n < 0)
            {
                LOG(LogLevel::ERROR) << "epoll_ctl error" ;
                return ;
            }
        }
    private:
        int _epfd;
    };
}